package com.wsc.wisdomchina.activitity.samemodule;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.wsc.wisdomchina.R;
import com.wsc.wisdomchina.Tools.JsonTools;
import com.wsc.wisdomchina.Tools.Tools;
import com.wsc.wisdomchina.activitity.baseactivity.BaseActivity;
import com.wsc.wisdomchina.activitity.fourmodule.FloorEventDetailActivity;
import com.wsc.wisdomchina.adapter.ActivityAdapter;
import com.wsc.wisdomchina.adapter.InfoListAdapter;
import com.wsc.wisdomchina.callback.ContributorView;
import com.wsc.wisdomchina.javaBean.ActivityListBean;
import com.wsc.wisdomchina.javaBean.InfoListBean;
import com.wsc.wisdomchina.javaBean.RespondObj;
import com.wsc.wisdomchina.presenter.ContributorPresenter;

import java.util.List;

/**
 * author: xw
 * created on: 2017/11/3 14:10
 * description: 资讯活动列表页
 */
public class InfoAndListActivity extends BaseActivity implements View.OnClickListener,ContributorView {

    private TextView fragment_lyhd_title;//标题
    private ImageView fragment_lyhd_back;//返回按钮
    private PullToRefreshListView fragment_lyhd_listView;//列表
    private Context context;
    private ListView actualListView;
    private int skip = 0;
    private final int limit = 6;
    private boolean isfresh = true;
    private ContributorPresenter contributorPresenter;
    private final String INFOLISTFLAG="INFOLISTFLAG";
    private final String ACTIVITYLISTFLAG="ACTIVITYLISTFLAG";
    private List<InfoListBean> infoListBeanList;
    private List<ActivityListBean> activityListBeanList;
    private InfoListAdapter infoListAdapter;
    private ActivityAdapter activityAdapter;
    private String cidNumber;
    private RelativeLayout layout;
    private String menuId;
    private boolean isInfo;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.floorevent_layout);
        context=this;
        initView();
        contributorPresenter=new ContributorPresenter(this,context);
        Intent intent=getIntent();
        fragment_lyhd_title.setText(intent.getStringExtra("name"));
        cidNumber=intent.getStringExtra("streetId");
        menuId=intent.getStringExtra("menuId");
        isInfo=intent.getBooleanExtra("isInfo",false);
        showData();
    }

    private void initView()
    {
        fragment_lyhd_title=(TextView) findViewById(R.id.tv_headtitle);//标题
        fragment_lyhd_back=(ImageView) findViewById(R.id.regist_back);//返回按钮
        fragment_lyhd_listView=(PullToRefreshListView) findViewById(R.id.fragment_lyhd_listView);//列表
        fragment_lyhd_back.setOnClickListener(this);

        layout=(RelativeLayout) findViewById(R.id.layout);

        fragment_lyhd_listView.setMode(PullToRefreshBase.Mode.BOTH);
        actualListView = fragment_lyhd_listView.getRefreshableView();

        actualListView.setOverScrollMode(View.OVER_SCROLL_NEVER);
        actualListView.setDivider(null);
        Tools.showRefreshView(fragment_lyhd_listView);

        fragment_lyhd_listView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {

            @Override
            public void onRefresh(PullToRefreshBase<ListView> refreshView) {
                // TODO Auto-generated method stub
                // Update the LastUpdatedLabe
                // 处理下拉刷新的业务
                if (refreshView.getCurrentMode().equals(PullToRefreshBase.Mode.PULL_FROM_START)) {
                    isfresh = true;
                    Tools.showRefreshView(fragment_lyhd_listView);
                    skip = 0;
                    showData();
                } else {
                    isfresh = false;
                    Tools.showRefreshView(fragment_lyhd_listView);
                    skip += limit;
                    showData();
                }

            }
        });

        actualListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                if(isInfo)
                {
                    //点击进入资讯详情
                    Intent intent = new Intent(context, InfoModuleDetailActivity.class);
                    intent.putExtra("id", infoListBeanList.get(position-1).getInformationId());
                    intent.putExtra("name",infoListBeanList.get(position-1).getShowColumn1());
                    startActivity(intent);
                }else {
                    //点击进入活动详情
                    Intent intent = new Intent(context, ActivityDetailActivity.class);
                    intent.putExtra("bid", activityListBeanList.get(position-1).getId());
                    intent.putExtra("cid",cidNumber);
                    intent.putExtra("type",0);
                    startActivity(intent);
                }
            }
        });
    }

    @Override
    public void onLoadContributorStart(String flag) {

    }


    @Override
    public void onLoadContributorSuccess(String jsonString, String flag) {
        try {
            //资讯
            if (flag.equals(INFOLISTFLAG)) {
                RespondObj respObj = JsonTools.jsonStringToObject(jsonString, RespondObj.class);
                String code = respObj.getCode();
                Object object = respObj.getObj();
                if (code.equals("200") && object != null) {
                    String jsonMessage = JsonTools.objectToJsonString(object);
                    List<InfoListBean> floorActivityListBeen = JsonTools.jsonStringToList(jsonMessage, InfoListBean.class);
                    if (floorActivityListBeen != null && floorActivityListBeen.size() > 0) {
                        if (isfresh) {
                            layout.setVisibility(View.GONE);
                            infoListBeanList = floorActivityListBeen;
                            infoListAdapter = new InfoListAdapter(context, infoListBeanList);
                            actualListView.setAdapter(infoListAdapter);
                        } else {
                            layout.setVisibility(View.GONE);
                            infoListBeanList.addAll(floorActivityListBeen);
                            infoListAdapter.notifyDataSetChanged();
                        }

                    }else
                    {
                        if(isfresh)
                        {
                            layout.setVisibility(View.VISIBLE);
                        }
                    }

                }
            }
            //活动
            else if(flag.equals(ACTIVITYLISTFLAG))
            {
                RespondObj respObj = JsonTools.jsonStringToObject(jsonString, RespondObj.class);
                String code = respObj.getCode();
                Object object = respObj.getObj();
                if (code.equals("200") && object != null) {
                    String jsonMessage = JsonTools.objectToJsonString(object);
                    List<ActivityListBean> floorActivityListBeen = JsonTools.jsonStringToList(jsonMessage, ActivityListBean.class);
                    if (floorActivityListBeen != null && floorActivityListBeen.size() > 0) {
                        if (isfresh) {
                            layout.setVisibility(View.GONE);
                            activityListBeanList = floorActivityListBeen;
                            activityAdapter = new ActivityAdapter(context, activityListBeanList);
                            actualListView.setAdapter(activityAdapter);
                        } else {
                            layout.setVisibility(View.GONE);
                            activityListBeanList.addAll(floorActivityListBeen);
                            activityAdapter.notifyDataSetChanged();
                        }

                    }else
                    {
                        if(isfresh)
                        {
                            layout.setVisibility(View.VISIBLE);
                        }
                    }

                }
            }
        } catch (Exception e) {

        }
    }


    @Override
    public void onLoadContributorComplete(String flag) {
        fragment_lyhd_listView.onRefreshComplete();
    }

    @Override
    public void onLoadContributorError(String error, String flag) {
    }

    private void showData() {
        if(isInfo)
        {
            //資訊
            contributorPresenter.getInformationList(cidNumber,menuId,skip,limit,INFOLISTFLAG);
        }else {
            //活動
            contributorPresenter.getActivityList(cidNumber,menuId,skip,limit,ACTIVITYLISTFLAG);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.regist_back:
                finish();
                break;
        }
    }
}
